package day07;
import java.util.*;

public class Person {
	private String name;              //人的名字
	private Calendar birthday;	   //该人的生日
	private boolean gender;	  //判断该人性别，真是男，假是女
	private Person partner;	     //判断该人是否已有配偶？
	
	public Person(String name, Calendar birthday, boolean gender) {
		super();
		this.name = name;
		this.birthday = birthday;
		this.gender = gender;
	}

	public boolean marry(Person p){
		if(this.gender==p.gender){
			System.out.println("同性不能结婚！");
			return false;
		}
		if(this.partner!=null || p.partner!=null){
			System.out.println("某一方已婚，不能再结婚！");
			return false;
		}
		if(!this.wasOverAgeForMarry()){
			System.out.println("对不起，您没有达到结婚年龄，不能结婚！");
			return false;
		}
		if(!p.wasOverAgeForMarry()){
			System.out.println("对不起，您的对象没有达到结婚年龄，不能结婚！");
			return false;
		}
		
		this.partner=p;
		p.partner=this;
		System.out.println("恭喜，"+this.name+"和"+p.name+"结婚成功！");
		return true;
	}
	
	public boolean wasOverAgeForMarry(){
		if(this.gender && this.getAge()>=22){
			return true;
		}
		if(!this.gender && this.getAge()>=20){
			return true;
		}
		return false;
	}
	
	public int getAge(){
		Calendar today=new GregorianCalendar();
		int todayYear=today.get(Calendar.YEAR);					
		int todayMonth=today.get(Calendar.MONTH);
		int todayDay=today.get(Calendar.DAY_OF_MONTH);
		
		int birthYear=birthday.get(Calendar.YEAR);
		int birthMonth=birthday.get(Calendar.MONTH);
		int birthDay=birthday.get(Calendar.DAY_OF_MONTH);
		
		if(todayMonth>birthMonth){
			return todayYear-birthYear;
		}else if(todayMonth<birthMonth){
			return todayYear-birthYear-1;
		}else if(todayDay>=birthDay){
			return todayYear-birthYear;
		}else{
			return todayYear-birthYear-1;
		}
	}

	public static void main(String[] args){
		Person p1=new Person("Wangpeihu",new GregorianCalendar(1986,5,6),true);
		Person p2=new Person("Ella",new GregorianCalendar(1988,3,11),false);
		Person p3=new Person("Selina",new GregorianCalendar(),false);
		p1.marry(p2);
		p3.marry(p2);
		p3.marry(p1);
	}

}
